package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.RegistLogin.AccountLoginActivity;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.utils.ToastUtil;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class ManagerUserPermissionActivity extends BaseWhiteActivity implements CompoundButton.OnCheckedChangeListener{
    private static final String TAG = AccountLoginActivity.class.getSimpleName();

    private ManagerUserInfo mInfo;
    private boolean canEdit = false;

    private AppCompatCheckBox cbLoader;
    private AppCompatCheckBox cbManager;
    private AppCompatCheckBox cbTermer;
    private CheckBox proXm;
    private CheckBox proJd;
    private CheckBox proZq;
    private CheckBox proBj;
    private CheckBox proTl;
    private CheckBox proRw;
    private CheckBox proGs;
    private Switch switch1;
    private Switch switch2;
    private Switch switch3;
    private Switch switch4;
    private Boolean groupleadflag = false;

    public static void start(Context context, ManagerUserInfo mInfo, boolean canEdit) {
        Intent intent = new Intent(context, ManagerUserPermissionActivity.class);
        intent.putExtra("info", new Gson().toJson(mInfo));
        intent.putExtra("canEdit", canEdit);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usermanager_permission, "成员角色权限");
        String bookJson = getIntent().getStringExtra("info");
        mInfo = new Gson().fromJson(bookJson, ManagerUserInfo.class);
        canEdit = getIntent().getBooleanExtra("canEdit", false);
        initView();
    }

    private void initView() {
        cbLoader = (AppCompatCheckBox) findViewById(R.id.cb_loader);
        cbManager = (AppCompatCheckBox) findViewById(R.id.cb_manager);
        cbTermer = (AppCompatCheckBox) findViewById(R.id.cb_termer);
        proXm = (CheckBox) findViewById(R.id.pro_xm);
        proJd = (CheckBox) findViewById(R.id.pro_jd);
        proZq = (CheckBox) findViewById(R.id.pro_zq);
        proBj = (CheckBox) findViewById(R.id.pro_bj);
        proTl = (CheckBox) findViewById(R.id.pro_tl);
        proRw = (CheckBox) findViewById(R.id.pro_rw);
        proGs = (CheckBox) findViewById(R.id.pro_gs);
        switch1 = (Switch) findViewById(R.id.switch1);
        switch2 = (Switch) findViewById(R.id.switch2);
        switch3 = (Switch) findViewById(R.id.switch3);
        switch4 = (Switch) findViewById(R.id.switch4);
        setData();
        checkEnable();
    }

    /**
     * 设置当前值
     */
    void setData(){
        int cb = mInfo.getProjectRole().getValue();
        String column = mInfo.getProjectColumnPermission();

        cbLoader.setChecked(cb == 101);
        cbManager.setChecked(cb == 102);
        cbTermer.setChecked(cb == 103);
        if(mInfo.getGroupLeader())
        {
            cbTermer.setChecked(true);
        }

        if(mInfo.getUserAuth()!=null &&!mInfo.getUserAuth().isDesignatedAdministrator())
        {
//            android:id="@+id/ll_cb_loader"
            findViewById(R.id.ll_cb_loader).setVisibility(View.GONE);
        }
        if(mInfo.getUserAuth()!=null &&!mInfo.getUserAuth().isDesignatedManager())
        {

            findViewById(R.id.ll_cb_manager).setVisibility(View.GONE);
        }

        proXm.setChecked(column.contains("0"));
        proJd.setChecked(column.contains("1"));
        proZq.setChecked(column.contains("2"));
        proBj.setChecked(column.contains("3"));
        proTl.setChecked(column.contains("4"));
        proRw.setChecked(column.contains("5"));
        proGs.setChecked(column.contains("6"));

        switch1.setChecked(mInfo.getOpManagementMembers().equals("true"));
        switch2.setChecked(mInfo.getOpOtherMembers().equals("true"));
        switch3.setChecked(mInfo.getContactCreditor());
        switch4.setChecked(mInfo.getGroupSentMessage().equals("true"));
    }

    /**
     * 检查是否可编辑
     */
    void checkEnable() {
        cbLoader.setEnabled(canEdit);
        cbManager.setEnabled(canEdit);
        cbTermer.setEnabled(canEdit);
        proXm.setEnabled(canEdit);
        proJd.setEnabled(canEdit);
        proZq.setEnabled(canEdit);
        proBj.setEnabled(canEdit);
        proTl.setEnabled(canEdit);
        proRw.setEnabled(canEdit);
        proGs.setEnabled(canEdit);
        switch1.setEnabled(canEdit);
        switch2.setEnabled(canEdit);
        switch3.setEnabled(canEdit);
        switch4.setEnabled(canEdit);

        if (canEdit) {
            cbLoader.setOnCheckedChangeListener(this);
            cbManager.setOnCheckedChangeListener(this);
            cbTermer.setOnCheckedChangeListener(this);
            addMenu(new MenuAction(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setPermission();
                }
            }, "完成", 0));
        }
    }

    private void setPermission() {
//        /open/api/managers/configurePermission
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            getToken(null);
            return;
        }

        int role = 0;
        if (cbLoader.isChecked())
            role = 101;
        else if (cbManager.isChecked() && cbTermer.isChecked())
        {
            role = 102;
            groupleadflag = true;
        }

        else if (cbManager.isChecked())
            role = 102;
        else if (cbTermer.isChecked()) {
            role = 103;
            groupleadflag = true;
        }
        else
            role = 104;

        String str = "";
        str += proXm.isChecked() ? ",0" : "";
        str += proJd.isChecked() ? ",1" : "";
        str += proZq.isChecked() ? ",2" : "";
        str += proBj.isChecked() ? ",3" : "";
        str += proTl.isChecked() ? ",4" : "";
        str += proRw.isChecked() ? ",5" : "";
        str += proGs.isChecked() ? ",6" : "";
        if (!TextUtils.isEmpty(str))
            str = str.substring(1);

        Network.getAPIService()
                .setPermission(mInfo.getId(), role, groupleadflag, str, switch1.isChecked(),
                        switch2.isChecked(), switch3.isChecked(), switch4.isChecked(), true, getUserToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code == 0) {
                            showToast("设置成功", ToastUtil.TOAST_TYPE.SUCCESS);
                            finish();
                        } else {
                            showToast("设置失败");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        showToast("设置失败");
                    }
                });
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.getId() == R.id.cb_loader && isChecked) {
            cbManager.setOnCheckedChangeListener(null);
            cbManager.setChecked(false);
            cbTermer.setOnCheckedChangeListener(null);
            cbTermer.setChecked(false);

            cbManager.setOnCheckedChangeListener(this);
            cbTermer.setOnCheckedChangeListener(this);

            proXm.setChecked(true);
            proJd.setChecked(true);
            proZq.setChecked(true);
            proBj.setChecked(true);
            proTl.setChecked(true);
            proRw.setChecked(true);
            proGs.setChecked(true);

            switch1.setChecked(true);
            switch2.setChecked(true);
            switch3.setChecked(true);
            switch4.setChecked(true);
        } else if (buttonView.getId() == R.id.cb_manager && isChecked) {
            if (cbLoader.isChecked()) {
                cbLoader.setOnCheckedChangeListener(null);
                cbLoader.setChecked(false);
                cbLoader.setOnCheckedChangeListener(this);
            }
            proXm.setChecked(true);
            proJd.setChecked(true);
            proZq.setChecked(true);
            proBj.setChecked(true);
            proTl.setChecked(true);
            proRw.setChecked(true);
            proGs.setChecked(true);

            switch1.setChecked(true);
            switch2.setChecked(true);
            switch3.setChecked(true);
            switch4.setChecked(true);
        } else if (buttonView.getId() == R.id.cb_termer && isChecked) {
            if (cbLoader.isChecked()) {
                cbLoader.setOnCheckedChangeListener(null);
                cbLoader.setChecked(false);
                cbLoader.setOnCheckedChangeListener(this);
            }

            if (!cbManager.isChecked()) {
                proXm.setChecked(true);
                proJd.setChecked(true);
                proZq.setChecked(false);
                proBj.setChecked(false);
                proTl.setChecked(false);
                proRw.setChecked(true);
                proGs.setChecked(false);

                switch1.setChecked(true);
                switch2.setChecked(false);
                switch3.setChecked(false);
                switch4.setChecked(false);
            }
        } else {
            if (cbTermer.isChecked()) {
                proXm.setChecked(true);
                proJd.setChecked(true);
                proZq.setChecked(false);
                proBj.setChecked(false);
                proTl.setChecked(false);
                proRw.setChecked(true);
                proGs.setChecked(false);

                switch1.setChecked(true);
                switch2.setChecked(false);
                switch3.setChecked(false);
                switch4.setChecked(false);
            }


            if (!cbLoader.isChecked() && !cbManager.isChecked() && !cbTermer.isChecked()) {
                switch1.setChecked(false);
                switch2.setChecked(false);
                switch3.setChecked(false);
                switch4.setChecked(false);

                proXm.setChecked(true);
                proJd.setChecked(true);
                proZq.setChecked(false);
                proBj.setChecked(false);
                proTl.setChecked(false);
                proRw.setChecked(false);
                proGs.setChecked(false);
            }
        }

        if(cbLoader.isChecked() || cbManager.isChecked())
        {
            proXm.setEnabled(false);
            proJd.setEnabled(false);
            proZq.setEnabled(false);
            proBj.setEnabled(false);
            proTl.setEnabled(false);
            proRw.setEnabled(false);
            proGs.setEnabled(false);

            switch1.setEnabled(false);
            switch2.setEnabled(false);
            switch3.setEnabled(false);
            switch4.setEnabled(false);
        }else{
            proXm.setEnabled(true);
            proJd.setEnabled(true);
            proZq.setEnabled(true);
            proBj.setEnabled(true);
            proTl.setEnabled(true);
            proRw.setEnabled(true);
            proGs.setEnabled(true);

            switch1.setEnabled(true);
            switch2.setEnabled(true);
            switch3.setEnabled(true);
            switch4.setEnabled(true);
        }

    }
}
